package com.smt.ejb3.tutoria.client;

import java.util.HashSet;
import java.util.Set;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.smt.ejb3.tutoria.entitybean.many2many.Role;
import com.smt.ejb3.tutoria.entitybean.many2many.Staff;
import com.smt.ejb3.tutoria.service.MappingService;

public class RoleClient {
	public static void main(String[] args) {
		
		Staff s1 = new Staff();
		s1.setName("staff 1");
		
		Staff s2 = new Staff();
		s2.setName("staff 2");
		
		Staff s3 = new Staff();
		s3.setName("staff 3");
		
		Staff s4 = new Staff();
		s4.setName("staff 4");
		
		Role r1 = new Role();
		r1.setName("Role 1");
		
		Role r2 = new Role();
		r2.setName("Role 2");
		
		Role r3 = new Role();
		r3.setName("Role 3");
		
		Set<Role> rs1 = new HashSet<Role>();
		rs1.add(r1);
		rs1.add(r3);
		
		Set<Role> rs2 = new HashSet<Role>();
		rs2.add(r1);
		rs2.add(r2);
		
		Set<Role> rs3 = new HashSet<Role>();
		rs3.add(r1);
		rs3.add(r2);
		
		Set<Role> rs4 = new HashSet<Role>();
		rs4.add(r2);
		rs4.add(r3);
		
		Set<Staff> ss1 = new HashSet<Staff>();
		ss1.add(s1);
		ss1.add(s2);
		ss1.add(s3);
		Set<Staff> ss2 = new HashSet<Staff>();
		ss2.add(s2);
		ss2.add(s3);
		ss2.add(s4);
		Set<Staff> ss3 = new HashSet<Staff>();
		ss3.add(s1);
		ss3.add(s4);
		
		r1.setStaffs(ss1);
		r2.setStaffs(ss2);
		r3.setStaffs(ss3);
		
		s1.setRoles(rs1);
		s2.setRoles(rs2);
		s3.setRoles(rs3);
		s4.setRoles(rs4);
		
		try {
			InitialContext ctx = new InitialContext();

			MappingService service = (MappingService) ctx
					.lookup("MappingServiceImpl/remote");
			/* Save roles */
//			service.saveRole(r1);
//			service.saveRole(r2);
//			service.saveRole(r3);
			/* Find roles */
//			Role role = service.findRole(1);
//			System.out.println(role.getStaffs());
			/* Find staff */
			Staff staff = service.findStaff("staff 1");
			for (Role role: staff.getRoles()){
				System.out.println("Role name: "+role.getName());
			}
			System.out.println("------------");
			staff = service.findStaff("staff 2");
			for (Role role: staff.getRoles()){
				System.out.println("Role name: "+role.getName());
			}
			System.out.println("------------");
			staff = service.findStaff("staff 3");
			for (Role role: staff.getRoles()){
				System.out.println("Role name: "+role.getName());
			}
			System.out.println("------------");
			staff = service.findStaff("staff 4");
			for (Role role: staff.getRoles()){
				System.out.println("Role name: "+role.getName());
			}
			System.out.println("------------");
			
		} catch (NamingException ex) {
			ex.printStackTrace();
		}
	}
}
